import {addBlacklist, deleteBlacklist, getListForPage} from '../../services/customer/blacklist';
import {message} from 'antd';

const R = require('ramda');

export default {
    namespace: 'blacklist',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, {payload}) {
            return {...state, ...payload};
        }
    },
    effects: {
        /**
         * 分页获取供应商列表
         *
         * @param call
         * @param put
         *
         */
        *getListForPage({payload}, {call, put}) {
            const res = yield call(getListForPage, {...R.omit(['onSuccess', 'onFail'])(payload)});

            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 新增黑名单
         *
         * @param call
         */
        *addBlacklist({payload}, {call}) {
            const res = yield call(addBlacklist, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("增加黑名单成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 删除黑名单
         *
         * @param id
         */
        *deleteBlacklist({payload}, {call}) {
            const res = yield call(deleteBlacklist, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("成功移除黑名单！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        }
    },

};